package com.chukun.gateway.core.processor;

import com.chukun.gateway.core.config.GatewayConfig;
import com.chukun.gateway.core.parallel.ParallelHandlerQueue;
import com.chukun.gateway.core.parallel.disruptor.DisruptorHandlerBuilder;
import com.chukun.gateway.core.request.HttpRequestWrapper;
import com.lmax.disruptor.dsl.ProducerType;

/**
 * @author chukun
 * @version 1.0.0
 * @description Disruptor流程处理类
 * @createTime 2023/12/23 20:39
 */
public class DisruptorNettyCoreProcessor implements NettyProcessor {

    private static  final String THREAD_NAME_PREFIX = "gateway-queue-";

    private GatewayConfig config;

    private ParallelHandlerQueue<HttpRequestWrapper> parallelQueueHandler;

    public DisruptorNettyCoreProcessor(GatewayConfig config, NettyCoreProcessor nettyCoreProcessor) {
        this.config = config;
        DisruptorHandlerBuilder<HttpRequestWrapper> builder = new DisruptorHandlerBuilder<HttpRequestWrapper>()
                .setBufferSize(config.getBufferSize())
                .setThreads(config.getProcessThread())
                .setProducerType(ProducerType.MULTI)
                .setNamePrefix(THREAD_NAME_PREFIX)
                .setWaitStrategy(config.getWaitStrategy());

        BatchEventListenerProcessor batchEventListenerProcessor = new BatchEventListenerProcessor(nettyCoreProcessor);
        builder.setEventListener(batchEventListenerProcessor);
        this.parallelQueueHandler = builder.builder();

    }


    @Override
    public void process(HttpRequestWrapper wrapper) {
        this.parallelQueueHandler.add(wrapper);
    }


}
